package cn.dxj1016.lesson03;

import cn.dxj1016.lesson02.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

public class TestInsert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JdbcUtils.getConnenction();//获取数据库连接
//            区别
//            使用？代替占位符参数
            String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,email,birthday) values (?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql);//预编译SQL，先写SQL，然后不执行
//            手动给参数赋值
            preparedStatement.setInt(1, 4);
            preparedStatement.setString(2, "qinjiang");
            preparedStatement.setString(3, "123456");
            preparedStatement.setString(4, "123344@qq.com");
//            注意点：sql.Date 数据库      java.sql.Date()
//                    util.Date Java     new Date().getTime() 获得时间戳
            preparedStatement.setDate(5,new java.sql.Date(new Date().getTime()));
//            执行
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("插入成功");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.release(connection,preparedStatement,null);
        }

    }
}
